package com.share.dubbo.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.common.common.Result;
import com.share.common.utils.DateUtil;
import com.share.common.utils.ObjUtil;
import com.share.dubbo.ssm.dao.mysql.AccessLogMapper;
import com.share.dubbo.ssm.manager.AccessLogManager;
import com.share.dubbo.ssm.model.AccessLogPageListReq;
import com.share.dubbo.ssm.model.AccessLogPageListResp;
import com.share.dubbo.ssm.model.AccessLogSaveReq;
import com.share.dubbo.ssm.model.mysql.AccessLog;
import com.share.dubbo.ssm.model.mysql.AccessLogExample;
import com.share.dubbo.ssm.model.req.CountByDayResq;
import com.share.dubbo.ssm.service.AccessLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 01401061 - 孙艳强
 * @date 2020/11/26 11:43
 */
@Path("accessLog")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Slf4j
@Component
public class AccessLogServiceImpl implements AccessLogService {

    @Autowired
    private AccessLogManager accessLogManager;

    @Autowired
    private AccessLogMapper accessLogMapper;

    @POST
    @Path("insert")
    @Override
    public Result<Integer> insert(AccessLogSaveReq req) {
        try {
            AccessLog record = new AccessLog();
            BeanUtils.copyProperties(req, record);
            record.setCreateTime(new Date());
            accessLogMapper.insertSelective(record);
            return Result.success(0);
        } catch (Exception e) {
            log.error("请求失败", e);
            return Result.failure(e.getMessage());
        }
    }

    @POST
    @Path("countToday")
    @Override
    public Result<Integer> countToday() {
        try {
            Integer count = null;
            String strDataYYYDDMM = DateUtil.getDate();
            List<CountByDayResq> list = accessLogMapper.countByDay(strDataYYYDDMM);
            if (list != null && list.size() > 0) {
                count = list.get(0).getCount();
            }
            return Result.success(count);
        } catch (Exception e) {
            log.error("请求失败", e);
            return Result.failure(e.getMessage());
        }
    }

    @POST
    @Path("pageList")
    @Override
    public Result<Page<AccessLogPageListResp>> pageList(AccessLogPageListReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageNum());
        AccessLogExample example = new AccessLogExample();
        if (StringUtils.isNoneBlank(req.getUrl())) {
            example.createCriteria().andUrlLike(req.getUrl());
        }
        List<AccessLog> list = accessLogMapper.selectByExample(example);

        List<AccessLogPageListResp> dtoList = new LinkedList<>();
        for (AccessLog record : list) {
            AccessLogPageListResp dto = ObjUtil.copy(record, AccessLogPageListResp.class);
            dtoList.add(dto);
        }
        return Result.success(new Page<>(dtoList, new PageInfo(dtoList).getTotal(), req));
    }

    @POST
    @Path("pageListLocal")
    @Override
    public Result<Page> pageListLocal(AccessLogPageListReq req) {
        Page<AccessLog> page = accessLogManager.pageListLocal(req);
        return Result.success(page);
    }
}
